package com.jams.goodox.activity.serve;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.listen.DialogActionListener;
import com.jams.goodox.listen.UploadListener;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.LifeService;
import com.jams.goodox.utils.RetrofitUtils;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.PromptBuilderDialog;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;
import uama.hangzhou.image.photochoose.PhotoChoose;
import uama.hangzhou.image.widget.MyGridView;

/**
 * 整租合租发布（个人）
 *
 * create by 顾佳佳 on 2017/05/05
 */
public class RentingPublishActivity extends BaseActivity {
    @Bind(R.id.et_group_publish_content)
    EditText etContent;
    @Bind(R.id.gv_group_publish_photo)
    MyGridView mGridView;
    @Bind(R.id.tv_renting_all)
    TextView tvRentingAll;
    @Bind(R.id.tv_renting_only)
    TextView tvRentingOnly;
    @Bind(R.id.tv_renting_send)
    TextView tvRentingSend;
    private PhotoChoose photoChoose;
    private int label = -1;//1-转租  2-合租
    private ProgressDialog progressDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_renting_publish;
    }

    @Override
    protected void init() {
        setTitleVisibility(false);
        photoChoose = new PhotoChoose(this, mGridView, 9);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        photoChoose.setImageList(requestCode, resultCode, data);
    }

    @OnClick({R.id.tv_renting_cancel, R.id.tv_renting_send, R.id.tv_renting_all, R.id.tv_renting_only})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_renting_cancel:
                close();
                break;
            case R.id.tv_renting_send:
                if (checkValidInput()) {
                    tvRentingSend.setClickable(false);
                    if (progressDialog == null) {
                        progressDialog = ProgressDialog.show(this, "", "", true, false);
                    } else {
                        progressDialog.show();
                    }
                    RetrofitUtils.uploadImage(this, photoChoose.getChosenImageList(), "neighbor", new UploadListener() {
                        @Override
                        public void success(List imageList) {
                            publish(imageList);
                        }

                        @Override
                        public void failure() {
                            progressDialog.dismiss();
                            tvRentingSend.setClickable(true);
                        }
                    });
                }
                break;
            case R.id.tv_renting_all:
                setLabel(1);
                break;
            case R.id.tv_renting_only:
                setLabel(2);
                break;
        }
    }

    private void setLabel(int label) {
        this.label = label;
        switch (label) {
            case 1://整租
                tvRentingAll.setTextColor(ContextCompat.getColor(this, R.color.font_main));
                tvRentingAll.setBackgroundResource(R.drawable.red_submit_btn_bg);
                tvRentingOnly.setTextColor(ContextCompat.getColor(this, R.color.font_hint));
                tvRentingOnly.setBackgroundResource(R.drawable.gray_stroke_bg);
                break;
            case 2://合租
                tvRentingOnly.setTextColor(ContextCompat.getColor(this, R.color.font_main));
                tvRentingOnly.setBackgroundResource(R.drawable.red_submit_btn_bg);
                tvRentingAll.setTextColor(ContextCompat.getColor(this, R.color.font_hint));
                tvRentingAll.setBackgroundResource(R.drawable.gray_stroke_bg);
                break;
        }
    }

    //检查页面输入
    private boolean checkValidInput() {
        if (label == -1) {
            ToastUtil.show(this, "请选择类型");
            return false;
        }
        if (TextUtils.isEmpty(etContent.getText())) {
            ToastUtil.show(this, "请输入房间描述");
            return false;
        }
        if (photoChoose.getChosenImageList().size() <= 0) {
            ToastUtil.show(this, "请上传房间图片");
            return false;
        }
        return true;
    }

    //finish检查
    private void close() {
        if (!TextUtils.isEmpty(etContent.getText().toString()) || photoChoose.getChosenImageList().size() > 0) {
            new PromptBuilderDialog(this).setContent(R.string.publish_drop_tips)
                    .setNegative(R.string.cancel)
                    .setPositive(R.string.confirm)
                    .setActionClickListener(new DialogActionListener() {
                        @Override
                        public void confirm(DialogInterface dialog, int which) {
                            finish();
                        }

                        @Override
                        public void cancel(DialogInterface dialog, int which) {
                            if (dialog != null)
                                dialog.dismiss();
                        }
                    }).show();
        }else {
            finish();
        }
    }

    //发布
    private void publish(List list) {
        enqueue(createCallService(LifeService.class).publishRenting(label == 1 ? "转租" :"合租", etContent.getText().toString(), list),
                new RetrofitCallbackListener() {
            @Override
            public void onSuccess(Call call, Object result) {
                progressDialog.dismiss();
                tvRentingSend.setClickable(true);
                ToastUtil.show(getString(R.string.publish_success));
                setResult(1992);
                finish();
            }

            @Override
            public void onError(String msg) {
                progressDialog.dismiss();
                tvRentingSend.setClickable(true);
            }
        });
    }

    @Override
    public void onBackPressed() {
        close();
    }
}
